﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace lcreport.DAL
{
	/// <summary>
	/// 数据访问类:reportinfo
	/// </summary>
	public partial class reportinfo
	{
		public reportinfo()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("rid", "reportinfo"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int rid)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from reportinfo");
			strSql.Append(" where rid=@rid ");
			SqlParameter[] parameters = {
					new SqlParameter("@rid", SqlDbType.Int,4)			};
			parameters[0].Value = rid;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(lcreport.Model.reportinfo model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into reportinfo(");
			strSql.Append("rname,rtel,remail,rdiqu,radd,rssd,rwork,robject,rtype,rtypesub,rtitle,rtext,rdate,uid,rfile1,rfile2,rfile3,rfile4,rfile5)");
			strSql.Append(" values (");
            strSql.Append("@rname,@rtel,@remail,@rdiqu,@radd,@rssd,@rwork,@robject,@rtype,@rtypesub,@rtitle,@rtext,@rdate,@uid,@rfile1,@rfile2,@rfile3,@rfile4,@rfile5);select @@IDENTITY");
			SqlParameter[] parameters = {					
					new SqlParameter("@rname", SqlDbType.NVarChar,20),
					new SqlParameter("@rtel", SqlDbType.NVarChar,20),
					new SqlParameter("@remail", SqlDbType.NVarChar,50),
					new SqlParameter("@rdiqu", SqlDbType.NVarChar,90),
					new SqlParameter("@radd", SqlDbType.NVarChar,100),
					new SqlParameter("@rssd", SqlDbType.NVarChar,100),
					new SqlParameter("@rwork", SqlDbType.NVarChar,100),
					new SqlParameter("@robject", SqlDbType.NVarChar,100),
					new SqlParameter("@rtype", SqlDbType.NVarChar,100),
					new SqlParameter("@rtypesub", SqlDbType.NVarChar,100),
					new SqlParameter("@rtitle", SqlDbType.NVarChar,90),
					new SqlParameter("@rtext", SqlDbType.Text),
					new SqlParameter("@rdate", SqlDbType.SmallDateTime),
					new SqlParameter("@uid", SqlDbType.Int,4),
					new SqlParameter("@rfile1", SqlDbType.NVarChar,50),
					new SqlParameter("@rfile2", SqlDbType.NVarChar,50),
					new SqlParameter("@rfile3", SqlDbType.NVarChar,50),
					new SqlParameter("@rfile4", SqlDbType.NVarChar,50),
					new SqlParameter("@rfile5", SqlDbType.NVarChar,50)};
            parameters[0].Value = model.rname;
            parameters[1].Value = model.rtel;
            parameters[2].Value = model.remail;
            parameters[3].Value = model.rdiqu;
            parameters[4].Value = model.radd;
            parameters[5].Value = model.rssd;
            parameters[6].Value = model.rwork;
            parameters[7].Value = model.robject;
            parameters[8].Value = model.rtype; 
			parameters[9].Value =  model.rtypesub;
			parameters[10].Value = model.rtitle;
			parameters[11].Value = model.rtext;
			parameters[12].Value = model.rdate;
			parameters[13].Value = model.uid;
			parameters[14].Value = model.rfile1;
			parameters[15].Value = model.rfile2;
			parameters[16].Value = model.rfile3;
			parameters[17].Value = model.rfile4;
			parameters[18].Value = model.rfile5;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
		}
        public bool Updatespwd(int id, string spwd)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update reportinfo set ");
            strSql.Append("spwd=@spwd ");            
            strSql.Append(" where rid=@rid ");
            SqlParameter[] parameters = {
					new SqlParameter("@spwd", SqlDbType.NVarChar,20),					
					new SqlParameter("@rid", SqlDbType.Int,4)};
            parameters[0].Value = spwd;
            parameters[1].Value = id;           
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(lcreport.Model.reportinfo model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update reportinfo set ");
			strSql.Append("rname=@rname,");
			strSql.Append("rtel=@rtel,");
			strSql.Append("remail=@remail,");
			strSql.Append("rdiqu=@rdiqu,");
			strSql.Append("radd=@radd,");
			strSql.Append("rssd=@rssd,");
			strSql.Append("rwork=@rwork,");
			strSql.Append("robject=@robject,");
			strSql.Append("rtype=@rtype,");
			strSql.Append("rtypesub=@rtypesub,");
			strSql.Append("rtitle=@rtitle,");
			strSql.Append("rtext=@rtext,");
			strSql.Append("rdate=@rdate,");
			strSql.Append("uid=@uid,");
			strSql.Append("rfile1=@rfile1,");
			strSql.Append("rfile2=@rfile2,");
			strSql.Append("rfile3=@rfile3,");
			strSql.Append("rfile4=@rfile4,");
			strSql.Append("rfile5=@rfile5,");
			strSql.Append("rresult=@rresult");
			strSql.Append(" where rid=@rid ");
			SqlParameter[] parameters = {
					new SqlParameter("@rname", SqlDbType.NVarChar,20),
					new SqlParameter("@rtel", SqlDbType.NVarChar,20),
					new SqlParameter("@remail", SqlDbType.NVarChar,50),
					new SqlParameter("@rdiqu", SqlDbType.NVarChar,90),
					new SqlParameter("@radd", SqlDbType.NVarChar,100),
					new SqlParameter("@rssd", SqlDbType.NVarChar,100),
					new SqlParameter("@rwork", SqlDbType.NVarChar,100),
					new SqlParameter("@robject", SqlDbType.NVarChar,100),
					new SqlParameter("@rtype", SqlDbType.NVarChar,100),
					new SqlParameter("@rtypesub", SqlDbType.NVarChar,100),
					new SqlParameter("@rtitle", SqlDbType.NVarChar,90),
					new SqlParameter("@rtext", SqlDbType.Text),
					new SqlParameter("@rdate", SqlDbType.SmallDateTime),
					new SqlParameter("@uid", SqlDbType.Int,4),
					new SqlParameter("@rfile1", SqlDbType.NVarChar,50),
					new SqlParameter("@rfile2", SqlDbType.NVarChar,50),
					new SqlParameter("@rfile3", SqlDbType.NVarChar,50),
					new SqlParameter("@rfile4", SqlDbType.NVarChar,50),
					new SqlParameter("@rfile5", SqlDbType.NVarChar,50),
					new SqlParameter("@rresult", SqlDbType.NVarChar,150),
					new SqlParameter("@rid", SqlDbType.Int,4)};
			parameters[0].Value = model.rname;
			parameters[1].Value = model.rtel;
			parameters[2].Value = model.remail;
			parameters[3].Value = model.rdiqu;
			parameters[4].Value = model.radd;
			parameters[5].Value = model.rssd;
			parameters[6].Value = model.rwork;
			parameters[7].Value = model.robject;
			parameters[8].Value = model.rtype;
			parameters[9].Value = model.rtypesub;
			parameters[10].Value = model.rtitle;
			parameters[11].Value = model.rtext;
			parameters[12].Value = model.rdate;
			parameters[13].Value = model.uid;
			parameters[14].Value = model.rfile1;
			parameters[15].Value = model.rfile2;
			parameters[16].Value = model.rfile3;
			parameters[17].Value = model.rfile4;
			parameters[18].Value = model.rfile5;
			parameters[19].Value = model.rresult;
			parameters[20].Value = model.rid;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int rid)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from reportinfo ");
			strSql.Append(" where rid=@rid ");
			SqlParameter[] parameters = {
					new SqlParameter("@rid", SqlDbType.Int,4)			};
			parameters[0].Value = rid;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
        public bool ResultList(string ridlist,string rresult)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update  reportinfo set rresult='" + rresult+"'");
            strSql.Append(" where rid in (" + ridlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string ridlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from reportinfo ");
			strSql.Append(" where rid in ("+ridlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public lcreport.Model.reportinfo GetModel(int rid)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 rid,rname,rtel,remail,rdiqu,radd,rssd,rwork,robject,rtype,rtypesub,rtitle,rtext,rdate,uid,rfile1,rfile2,rfile3,rfile4,rfile5,rresult from reportinfo ");
			strSql.Append(" where rid=@rid ");
			SqlParameter[] parameters = {
					new SqlParameter("@rid", SqlDbType.Int,4)			};
			parameters[0].Value = rid;

			lcreport.Model.reportinfo model=new lcreport.Model.reportinfo();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public lcreport.Model.reportinfo DataRowToModel(DataRow row)
		{
			lcreport.Model.reportinfo model=new lcreport.Model.reportinfo();
			if (row != null)
			{
				if(row["rid"]!=null && row["rid"].ToString()!="")
				{
					model.rid=int.Parse(row["rid"].ToString());
				}
				if(row["rname"]!=null)
				{
					model.rname=row["rname"].ToString();
				}
				if(row["rtel"]!=null)
				{
					model.rtel=row["rtel"].ToString();
				}
				if(row["remail"]!=null)
				{
					model.remail=row["remail"].ToString();
				}
				if(row["rdiqu"]!=null)
				{
					model.rdiqu=row["rdiqu"].ToString();
				}
				if(row["radd"]!=null)
				{
					model.radd=row["radd"].ToString();
				}
				if(row["rssd"]!=null)
				{
					model.rssd=row["rssd"].ToString();
				}
				if(row["rwork"]!=null)
				{
					model.rwork=row["rwork"].ToString();
				}
				if(row["robject"]!=null)
				{
					model.robject=row["robject"].ToString();
				}
				if(row["rtype"]!=null)
				{
					model.rtype=row["rtype"].ToString();
				}
				if(row["rtypesub"]!=null)
				{
					model.rtypesub=row["rtypesub"].ToString();
				}
				if(row["rtitle"]!=null)
				{
					model.rtitle=row["rtitle"].ToString();
				}
				if(row["rtext"]!=null)
				{
					model.rtext=row["rtext"].ToString();
				}
				if(row["rdate"]!=null && row["rdate"].ToString()!="")
				{
					model.rdate=DateTime.Parse(row["rdate"].ToString());
				}
				if(row["uid"]!=null && row["uid"].ToString()!="")
				{
					model.uid=int.Parse(row["uid"].ToString());
				}
				if(row["rfile1"]!=null)
				{
					model.rfile1=row["rfile1"].ToString();
				}
				if(row["rfile2"]!=null)
				{
					model.rfile2=row["rfile2"].ToString();
				}
				if(row["rfile3"]!=null)
				{
					model.rfile3=row["rfile3"].ToString();
				}
				if(row["rfile4"]!=null)
				{
					model.rfile4=row["rfile4"].ToString();
				}
				if(row["rfile5"]!=null)
				{
					model.rfile5=row["rfile5"].ToString();
				}
				if(row["rresult"]!=null)
				{
					model.rresult=row["rresult"].ToString();
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select rid,rname,rtel,remail,rdiqu,radd,rssd,rwork,robject,rtype,rtypesub,rtitle,rtext,rdate,uid,rfile1,rfile2,rfile3,rfile4,rfile5,rresult ");
			strSql.Append(" FROM reportinfo where 1=1");
			if(strWhere.Trim()!="")
			{
				strSql.Append(strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" rid,rname,rtel,remail,rdiqu,radd,rssd,rwork,robject,rtype,rtypesub,rtitle,rtext,rdate,uid,rfile1,rfile2,rfile3,rfile4,rfile5,rresult ");
			strSql.Append(" FROM reportinfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM reportinfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.rid desc");
			}
			strSql.Append(")AS Row, T.*  from reportinfo T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "reportinfo";
			parameters[1].Value = "rid";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

